package com.gitee.hljdrl.utils.string;

import android.text.TextUtils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringUtil {

    public static String getStackTraceString(Throwable tr) {
        try {
            if (tr == null) {
                return "";
            }

            // This is to reduce the amount of log spew that apps do in the non-error
            // condition of the network being unavailable.
            Throwable t = tr;
            while (t != null) {
                if (t instanceof UnknownHostException) {
                    return "UnknownHostException";
                }
                t = t.getCause();
            }

            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            tr.printStackTrace(pw);
            pw.flush();
            return sw.toString();
        } catch (Exception var3) {
            return "Error";
        }
    }

    public static String string(String... str) {
        StringBuffer buffer = new StringBuffer();
        if (str != null) {
            for (String _s : str) {
                buffer.append(_s);
            }
        }
        return buffer.toString();
    }

    public static String string(Object... str) {
        StringBuffer buffer = new StringBuffer();
        if (str != null) {
            for (Object _s : str) {
                if (_s != null) {
                    buffer.append(_s);
                }
            }
        }
        return buffer.toString();
    }

    public static boolean isHttp(String var) {
        if (TextUtils.isEmpty(var)) {
            return Boolean.FALSE;
        }
        if (var.startsWith("http")) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    public static String value(String str, String defaultValue) {
        if (TextUtils.isEmpty(str)) {
            return defaultValue;
        }
        return str;
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

}
